package org.x.usermod.service.impl;


import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.x.commonmod.enums.LogAsync;
import org.x.commonmod.config.Result;
import org.x.usermod.dto.SysAclCreateDTO;
import org.x.usermod.dto.SysAclDTO;
import org.x.usermod.dto.SysAclListDTO;
import org.x.usermod.dto.SysAclUpdateDTO;
import org.x.usermod.service.ISysAclService;
import org.x.usermod.entity.SysAclEntity;
import org.x.usermod.mapper.SysAclMapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.x.usermod.vo.SysAclListVO;
import ch.qos.logback.core.util.StringUtil;

import java.util.Date;
import java.util.List;

/**
 * 权限表 服务层实现。
 *
 * @author mybatis-flex-helper automatic generation
 * @since 1.0
 */
@Service
public class SysAclServiceImpl extends ServiceImpl<SysAclMapper, SysAclEntity> implements ISysAclService {

    @Autowired
    private SysAclMapper sysAclMapper;

    /**
     * 分页查询权限表
     *
     * @param sysAclListDTO
     * @return
     */
    @LogAsync("分页查询权限表")
    @Override
    public Page<SysAclListVO> pagelist(SysAclListDTO sysAclListDTO) {
        //检验SysAclListDTO是否为空，不为空才构建查询条件
        QueryWrapper wrapper = new QueryWrapper();
        if(!StringUtil.isNullOrEmpty(sysAclListDTO.getName()))
        {
            wrapper.like(SysAclEntity::getName, sysAclListDTO.getName());
        }
        if(!StringUtil.isNullOrEmpty(sysAclListDTO.getCode()))
        {
            wrapper.like(SysAclEntity::getCode, sysAclListDTO.getCode());
        }
        if(!StringUtil.isNullOrEmpty(sysAclListDTO.getType()))
        {
            wrapper.like(SysAclEntity::getType, sysAclListDTO.getType());
        }
        if(!StringUtil.isNullOrEmpty(sysAclListDTO.getUrl()))
        {
            wrapper.like(SysAclEntity::getUrl, sysAclListDTO.getUrl());
        }
        if(!StringUtil.isNullOrEmpty(String.valueOf(sysAclListDTO.getIsDelete())))
        {
            wrapper.like(SysAclEntity::getIsDelete, sysAclListDTO.getIsDelete());
        }

        Page<SysAclEntity> page = sysAclMapper.paginate(
                sysAclListDTO.getPage(),
                sysAclListDTO.getSize(),
                wrapper
        );
        //paginate方法返回的是Page<T>，需要转换成Page<SysUserVo>
        Page<SysAclListVO> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        return pageVo;

    }

    /**
     * (编辑)更新权限表
     *
     * @param sysAclUpdateDTO
     * @return
     */

    @LogAsync("更新权限表")
    @Override
    public String update(SysAclUpdateDTO sysAclUpdateDTO) {
        //检验权限表是否存在
        SysAclEntity acl = sysAclMapper.selectOneById(sysAclUpdateDTO.getId());
        if (acl != null)
        {
            BeanUtils.copyProperties(sysAclUpdateDTO, acl);
            acl.setUpdatedTime( new Date());
            sysAclMapper.insertOrUpdateSelective(acl);
            return "更新成功";
        }
        return "更新失败";
    }

    /**
     * 逻辑删除权限表
     * 设置isDelete=1
     * @param id
     * @return
     */
    @LogAsync("逻辑删除权限表")
    @Override
    public Result removeById(Integer id) {
        //逻辑删除，设置is_delete=1
        //检验权限表是否存在
        SysAclEntity acl = sysAclMapper.selectOneById(id);
        if (acl != null)
        {
            acl.setIsDelete(1);
            sysAclMapper.insertOrUpdateSelective(acl);
            return Result.ok("删除成功");
        }
        return Result.fail("500", "删除失败");
    }

    /**
     * 新增权限表
     *
     * @param sysAclCreateDTO
     * @return
     */
    @LogAsync("新增权限表")
    @Override
    public String create(SysAclCreateDTO sysAclCreateDTO) {
        SysAclEntity acl = new SysAclEntity();
        BeanUtils.copyProperties(sysAclCreateDTO, acl);
        acl.setIsDelete(0);
        acl.setCreatedTime(new Date());
        sysAclMapper.insert(acl);
        return "添加成功";
    }

    /**
     * 查询权限表
     *
     * @param sysAclDTO
     * @return
     */

    @Override
    public List<SysAclEntity> list(SysAclDTO sysAclDTO) {
        //查询所有权限表
        List<SysAclEntity> acls = sysAclMapper.selectAll();
        return acls;

    }

}